﻿//********************************************************************************//
//创建日期：<创建日期，2016-09-19>
//创建作者：<孙雪婷，sunxueting@shanghai3h.com>
//功能说明：专灌计划 专灌计划输入
//********************************************************************************//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Platform.Http;
using System.Text;
using Web.Class;

namespace Web.Pages.QuShui.DiXiaS.JiHuaGL.ZhuanGuanJH
{
    public partial class ZhuanGuanJHSR : System.Web.UI.Page
    {
        #region 属性

        /// <summary>
        /// 保存结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        #endregion

        #region 变量
        /// <summary>
        /// 专灌计划
        /// </summary>
        QS_JH_ZHUANGUANJH _ZGJH = new QS_JH_ZHUANGUANJH();

        /// <summary>
        /// 专灌计划bll
        /// </summary>
        QS_JH_ZHUANGUANJHBLL _ZGJHBLL = new QS_JH_ZHUANGUANJHBLL();

        /// <summary>
        /// 存储过程执行类
        /// </summary>
        SpHelper _SpHelper = new SpHelper();

        /// <summary>
        /// 操作日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        /// <summary>
        /// 参数bll
        /// </summary>
        QS_CANSHUBBLL _CANSHUBLL = new QS_CANSHUBBLL();
        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitData();
            }
            LoadData();
        }

        /// <summary>
        /// 绑定空表头
        /// </summary>
        protected override void Render(HtmlTextWriter sender)
        {
            base.Render(sender);
        }

        /// <summary>
        /// 年份切换事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cmbNianF1_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        /// <summary>
        /// 响应前台回调
        /// </summary>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            int id = Convert.ToInt32(this.hdCollection.Get("ID"));
            string msg = "";
            if (e.Parameter == "setinfo")
            {
                if (id != -1)
                {
                    int year = Convert.ToInt32(cmbNianF1.Value);
                    DataTable dt_temp = _SpHelper.P_QSJH_JIHUASR(2,id, year).Tables[0];
                    int zgjh = Convert.ToInt32(dt_temp.Rows[0]["I_ZHUANGUANJH"]) / 10000;
                    msg = string.Join("|", Convert.ToString(dt_temp.Rows[0]["S_BUMENBH"]), Convert.ToString(dt_temp.Rows[0]["S_BUMENMC"]),
                        Convert.ToString(zgjh));
                }
                e.Result = e.Parameter + "|0|" + msg;
            }
            else if (e.Parameter == "save")
            {
                int val = Convert.ToInt32(cmbShuRu.Value);//1 万立方米，2 立方米
                OperateState operateState;
                _ZGJH = _ZGJHBLL.SelectQS_JH_ZHUANGUANJH(id);
                operateState = OperateState.Modify;
                _ZGJH.I_ZHUANGUANJH = val == 1 ? (Convert.ToDecimal(txtJiHuaL.Text) * 10000) : Convert.ToDecimal(txtJiHuaL.Text);
                _ZGJH.I_JLZT = 0;
                _ZGJH.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                _ZGJH.D_CAOZUORQ = BusinessTool.GetDataBaseDateTime();
                _SYS_LOGBLL.Log<QS_JH_ZHUANGUANJH>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "专灌计划输入", operateState, _ZGJH, null);
                bool result = _ZGJHBLL.InsertOrUpdate(_ZGJH);
                if (result)
                {
                    msg = "true";
                    
                    e.Result = e.Parameter + "|0|" + msg;
                }
            }
          
        }



        /// <summary>
        /// 客户端刷新list
        /// </summary>
        protected void gvList_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        {
            LoadData();
        }

        #endregion

        #region 方法

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            int year = DateTime.Now.Year;
            for (int i = year - 10; i <= year; i++)
            {
                cmbNianF1.Items.Add(i.ToString(), i);
            }
            cmbNianF1.SelectedIndex = 10;
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData()
        {
            int year = Convert.ToInt32(cmbNianF1.Value);
            this.dtSource = _SpHelper.P_QSJH_JIHUASR(2,0, year).Tables[0];
            gvList.DataSource = this.dtSource;
            gvList.DataBind();
            int HJ = 0;
            for (int i = 0; i < this.dtSource.Rows.Count; i++)
            {
                HJ += Convert.ToInt32(this.dtSource.Rows[i]["I_ZHUANGUANJH"]);
            }
            lbHeJi.Text = "计划量合计" + HJ.ToString() + "立方米";
            lbcount.Text = "共" + this.dtSource.Rows.Count.ToString() + "条";
        }

        #endregion        
    }
}